<html>
<head>
  <title>01 网络基础概念</title>
  <basefont face="微软雅黑" size="2" />
  <meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
  <meta name="exporter-version" content="Evernote Windows/306387 (zh-CN, DDL); Windows/10.0.0 (Win64);"/>
  <style>
    body, td {
      font-family: 微软雅黑;
      font-size: 12pt;
    }
  </style>
</head>
<body>
<a name="3183"/>
<h1>01 网络基础概念</h1>

<div>
<span><div style="box-sizing: border-box; margin: 0px; padding: 0px; font-size: medium; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px;"><span style="box-sizing: border-box; font-size: medium; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; color: rgb(0, 0, 0); font-family: 微软雅黑; font-variant-caps: normal; font-variant-ligatures: normal; font-weight: bold;">课程安排</span></div><div style="box-sizing: border-box; margin: 0px; padding: 0px; font-size: medium; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px;"><span style="box-sizing: border-box; font-size: medium; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; color: rgb(0, 0, 0); font-family: 微软雅黑; font-variant-caps: normal; font-variant-ligatures: normal; font-weight: bold;"> </span><span style="box-sizing: border-box; font-size: medium; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; color: rgb(0, 0, 0); font-family: 微软雅黑; font-variant-caps: normal; font-variant-ligatures: normal;"> </span><span style="box-sizing: border-box; font-size: medium; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; color: rgb(0, 0, 0); font-family: 微软雅黑; font-variant-caps: normal; font-variant-ligatures: normal;">  网编编程 5d</span></div><div style="box-sizing: border-box; margin: 0px; padding: 0px; font-size: medium; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px;"><span style="box-sizing: border-box; font-size: medium; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; color: rgb(0, 0, 0); font-family: 微软雅黑; font-variant-caps: normal; font-variant-ligatures: normal;">        第一天:网络基础 socket编程</span></div><div style="box-sizing: border-box; margin: 0px; padding: 0px; font-size: medium; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px;"><span style="box-sizing: border-box; font-size: medium; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; color: rgb(0, 0, 0); font-family: 微软雅黑; font-variant-caps: normal; font-variant-ligatures: normal;">        第二天:tcp三次握手    高并发服务器(多进程多线程)</span></div><div style="box-sizing: border-box; margin: 0px; padding: 0px; font-size: medium; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px;"><span style="box-sizing: border-box; font-size: medium; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; color: rgb(0, 0, 0); font-family: 微软雅黑; font-variant-caps: normal; font-variant-ligatures: normal;">        第三天:tcp转换状态    高并发服务器(select poll epoll)</span></div><div style="box-sizing: border-box; margin: 0px; padding: 0px; font-size: medium; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px;"><span style="box-sizing: border-box; font-size: medium; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; color: rgb(0, 0, 0); font-family: 微软雅黑; font-variant-caps: normal; font-variant-ligatures: normal;">        第四天: 高并发服务器( poll epoll)  epoll反应堆</span></div><div style="box-sizing: border-box; margin: 0px; padding: 0px; font-size: medium; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px;"><span style="box-sizing: border-box; font-size: medium; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; color: rgb(0, 0, 0); font-family: 微软雅黑; font-variant-caps: normal; font-variant-ligatures: normal;">        第五天: 线程池  udp网编编程  本地套接字</span></div><div style="box-sizing: border-box; margin: 0px; padding: 0px; font-size: medium; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px;"><span style="box-sizing: border-box; font-size: medium; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; color: rgb(0, 0, 0); font-family: 微软雅黑; font-variant-caps: normal; font-variant-ligatures: normal;">    libevnet第三方网络库编程 1d</span></div><div style="box-sizing: border-box; margin: 0px; padding: 0px; font-size: medium; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px;"><span style="box-sizing: border-box; font-size: medium; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; color: rgb(0, 0, 0); font-family: 微软雅黑; font-variant-caps: normal; font-variant-ligatures: normal;">    web-server服务器开发 2d</span></div><div style="box-sizing: border-box; margin: 0px; padding: 0px; font-size: medium; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px;"><span style="box-sizing: border-box; font-size: medium; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; color: rgb(0, 0, 0); font-family: 微软雅黑; font-variant-caps: normal; font-variant-ligatures: normal;">   </span></div><div style="box-sizing: border-box; margin: 0px; padding: 0px; font-size: medium; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px;"><br style="box-sizing: border-box;"/></div><div style="box-sizing: border-box; margin: 0px; padding: 0px; font-size: medium; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px;"><hr style="box-sizing: border-box; margin: 1px 0px; padding-top: 1px; border-width: 6px 0px; border-style: solid none; border-color: white; background-color: rgb(153, 153, 153);"/></div><div style="box-sizing: border-box; margin: 0px; padding: 0px; font-size: medium; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px;"><br style="box-sizing: border-box;"/></div><div style="box-sizing: border-box; margin: 0px; padding: 0px; font-size: medium; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px;"><span style="box-sizing: border-box; font-size: medium; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; color: rgb(0, 0, 0); font-family: 微软雅黑; font-variant-caps: normal; font-variant-ligatures: normal; font-weight: bold;">第一天学习内容</span></div><div style="box-sizing: border-box; margin: 0px; padding: 0px; font-size: medium; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px;"><span style="box-sizing: border-box; font-size: medium; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; color: rgb(0, 0, 0); font-family: 微软雅黑; font-variant-caps: normal; font-variant-ligatures: normal;">    1    了解基础网络mac,ip,port</span></div><div style="box-sizing: border-box; margin: 0px; padding: 0px; font-size: medium; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px;"><span style="box-sizing: border-box; font-size: medium; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; color: rgb(0, 0, 0); font-family: 微软雅黑; font-variant-caps: normal; font-variant-ligatures: normal;">    2    了解OSI七层模型</span></div><div style="box-sizing: border-box; margin: 0px; padding: 0px; font-size: medium; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px;"><span style="box-sizing: border-box; font-size: medium; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; color: rgb(0, 0, 0); font-family: 微软雅黑; font-variant-caps: normal; font-variant-ligatures: normal;">    3   了解网络常见协议格式</span></div><div style="box-sizing: border-box; margin: 0px; padding: 0px; font-size: medium; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px;"><span style="box-sizing: border-box; font-size: medium; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; color: rgb(0, 0, 0); font-family: 微软雅黑; font-variant-caps: normal; font-variant-ligatures: normal;">    4    掌握网络字节序和主机字节序之间的转换</span></div><div style="box-sizing: border-box; margin: 0px; padding: 0px; font-size: medium; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px;"><span style="box-sizing: border-box; font-size: medium; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; color: rgb(0, 0, 0); font-family: 微软雅黑; font-variant-caps: normal; font-variant-ligatures: normal;">    5    tcp服务器通信流程</span></div><div style="box-sizing: border-box; margin: 0px; padding: 0px; font-size: medium; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px;"><span style="box-sizing: border-box; font-size: medium; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; color: rgb(0, 0, 0); font-family: 微软雅黑; font-variant-caps: normal; font-variant-ligatures: normal;">    6    tcp客户端通信流程</span></div><div style="box-sizing: border-box; margin: 0px; padding: 0px; font-size: medium; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px;"><span style="box-sizing: border-box; font-size: medium; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; color: rgb(0, 0, 0); font-family: 微软雅黑; font-variant-caps: normal; font-variant-ligatures: normal;">    7    独立写出tcp服务器端代码</span></div><div><span style="box-sizing: border-box; font-size: medium; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; color: rgb(0, 0, 0); font-family: 微软雅黑; font-variant-caps: normal; font-variant-ligatures: normal;">    8    独立写出tcp客户端代码</span></div><div><br/></div><div><br/></div><div><br/></div><div><span style="box-sizing: border-box; font-size: medium; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; color: rgb(0, 0, 0); font-family: 微软雅黑; font-variant-caps: normal; font-variant-ligatures: normal;">1 网卡</span></div><div>网络适配器  :作用  收发数据</div><div>mac地址  作用: 用来标识一块网卡   6个字节  物理地址</div><div><img src="01 网络基础概念_files/Image.png" type="image/png" data-filename="Image.png"/></div><div><br/></div><div><span style="box-sizing: border-box; font-size: medium; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; color: rgb(0, 0, 0); font-family: 微软雅黑; font-variant-caps: normal; font-variant-ligatures: normal;">2  ip</span></div><div>ip用来标识一台主机  逻辑地址</div><div>iPv4   :   ip地址是4字节 32位</div><div>ipv6:     128位   16字节</div><div>子网id    ip中被子网掩码中1连续覆盖的位</div><div>主机id   ip中被子网掩码中0连续覆盖的位</div><div>192.168.1.2/24   192.168.1.2/255.255.255.0</div><div><img src="01 网络基础概念_files/Image [1].png" type="image/png" data-filename="Image.png"/></div><div><br/></div><div><br/></div><div>网段地址: 192.168.1.0</div><div>广播地址: 192.168.1.255</div><div><br/></div><div>子网掩码  netmask: 用来区分子网id 和主机id</div><div><br/></div><div><span style="box-sizing: border-box; font-size: medium; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; color: rgb(0, 0, 0); font-family: 微软雅黑; font-variant-caps: normal; font-variant-ligatures: normal;">3 端口</span></div><div>作用: 用来标识应用程序(进程)</div><div>port: 2个字节  0-65535</div><div>0-1023 知名端口</div><div>自定义端口 1024 - 65535</div><div>netstat</div><div><br/></div><div><span style="box-sizing: border-box; font-size: medium; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; color: rgb(0, 0, 0); font-family: 微软雅黑; font-variant-caps: normal; font-variant-ligatures: normal;">4 OSI 七层模型</span></div><div>物理层:  双绞线接口类型, 光纤的传输速率等等</div><div>数据链路层:  mac 负责收发数据</div><div>网络层: ip 给两台主机提供路径选择</div><div>传输层:  port  区分数据递送到哪一个应用程序</div><div>会话层: 建立建立</div><div>表示层: 解码 </div><div>应用层</div><div><br/></div><div><img src="01 网络基础概念_files/Image [2].png" type="image/png" data-filename="Image.png" width="518"/></div><div><br/></div><div>TCP/IP四层模型</div><div><img src="01 网络基础概念_files/Image [3].png" type="image/png" data-filename="Image.png"/></div><div><span style="box-sizing: border-box; font-size: medium; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; color: rgb(0, 0, 0); font-family: 微软雅黑; font-variant-caps: normal; font-variant-ligatures: normal;">5 协议</span></div><div>规定了数据传输的方式和格式</div><div><br/></div><div>应用层协议:  </div><div>FTP: 文件传输协议</div><div>HTTP: 超文本传输协议</div><div>NFS:  网络文件系统</div><div>传输层协议:</div><div>TCP:   传输控制协议</div><div>UDP:  用户数据报协议</div><div>网络层:</div><div>IP:英特网互联协议</div><div>ICMP:  英特网控制报文协议  ping</div><div>IGMP:  英特网组管理协议</div><div>链路层协议:</div><div>ARP:  地址解析协议 通过ip找mac地址</div><div>RARP: 反向地址解析协议 通过mac找ip</div><div><br/></div><div>6 网络通信过程</div><div><img src="01 网络基础概念_files/Image [4].png" type="image/png" data-filename="Image.png"/></div><div><br/></div><div><br/></div><div>7 arp</div><div>地址解析协议: 通过ip找mac地址</div><div><img src="01 网络基础概念_files/Image [5].png" type="image/png" data-filename="Image.png"/></div><div>arp请求包:</div><div><br/></div><div><img src="01 网络基础概念_files/Image [6].png" type="image/png" data-filename="Image.png"/></div><div><br/></div><div>8 网络设计模式</div><div>B/S  browser/ server</div><div>C/S  cilent/server </div><div><br/></div><div>c/s  性能较好 客户端容易篡改数据   开发周期较长</div><div>b/s  性能低   客户端安全  开发周期短</div><div><br/></div><div>9 进程间通信</div><div>无名管道</div><div>命名管道</div><div>mmap</div><div>文件</div><div>信号</div><div>消息队列</div><div>共享内存</div><div>只能用于 本机的进程间通信</div><div><br/></div><div>不同的主机间进程通信方法:  socket</div><div>socket必须成对出现</div><div><img src="01 网络基础概念_files/Image [7].png" type="image/png" data-filename="Image.png"/></div><div><br/></div><div>10 字节序</div><div><img src="01 网络基础概念_files/Image [8].png" type="image/png" data-filename="Image.png"/></div><div><br/></div><div>11 ip转换</div><div>#include &lt;arpa/inet.h&gt;</div><div> int inet_pton(int af, const char *src, void *dst);<br/></div><div>功能: 将点分十进制串 转成32位网络大端的数据(&quot;192.168.1.2&quot;  ==&gt;   )</div><div>参数:</div><div><span>    af : </span><br/></div><div><span><span>    </span><span>    AF_INET         IPV4</span><br/></span></div><div><span><span>    </span><span>    AF_INET6        IPV6</span><br/></span></div><div><span> </span> src: 点分十进制串的首地址 <br/></div><div><span>  dst : 32位网络数据的地址 </span></div><div><span>成功返回1</span></div><div><span><br/></span></div><div><span>]</span></div><div>#include &lt;arpa/inet.h&gt;</div><div>const char *inet_ntop(int af, const void *src,<br/></div><div>                             char *dst, socklen_t size);</div><div>功能: 将32位大端的网络数据转成点分十进制串</div><div>参数:</div><div><span>    af : AF_INET</span><br/></div><div><span>    src : 32位大端的网络数 地址</span><br/></div><div><span><span>    dst : 存储点分十进制串 地址</span><br/></span></div><div><span><span>    size : 存储点分制串数组的大小  </span><br/></span></div><div>返回值: 存储点分制串数组首地址</div><div><br/></div><div>12  网络通信解决三大问题</div><div>协议</div><div>ip</div><div>端口</div><div><br/></div><div>ipv4套接字结构体</div><div>struct sockaddr_in {</div><div>               sa_family_t    sin_family; /* address family: AF_INET */</div><div>               in_port_t      sin_port;   /* port in network byte order */</div><div>               struct in_addr sin_addr;   /* internet address */</div><div>           };</div><div><br/></div><div>           /* Internet address. */</div><div>           struct in_addr {</div><div>               uint32_t       s_addr;     /* address in network byte order */</div><div>           };</div><div><br/></div><div>sin_family: 协议  AF_INET</div><div>sin_portL端口</div><div><span> sin_addr  ip地址</span><br/></div><div><br/></div><div><br/></div><div>ipv6套接字结构体</div><div style="margin-top: 1em; margin-bottom: 1em;"><span style="-en-paragraph:true;">struct sockaddr_in6 {</span></div><div style="margin-top: 1em; margin-bottom: 1em;"><span style="mso-tab-count: 1;-en-paragraph:true;">   </span> <span style="-en-paragraph:true;">unsigned short int sin6_family;</span> <span style="mso-tab-count: 2;-en-paragraph:true;">      </span> <span style="-en-paragraph:true;">/* AF_INET6 */</span></div><div style="margin-top: 1em; margin-bottom: 1em;"><span style="mso-tab-count: 1;-en-paragraph:true;">   </span> <span style="-en-paragraph:true;">__be16 sin6_port;</span> <span style="mso-tab-count: 5;-en-paragraph:true;">                </span> <span style="-en-paragraph:true;">/* Transport layer port # */</span></div><div style="margin-top: 1em; margin-bottom: 1em;"><span style="mso-tab-count: 1;-en-paragraph:true;">   </span> <span style="-en-paragraph:true;">__be32 sin6_flowinfo;</span> <span style="mso-tab-count: 4;-en-paragraph:true;">            </span> <span style="-en-paragraph:true;">/* IPv6 flow information */</span></div><div style="margin-top: 1em; margin-bottom: 1em;"><span style="mso-tab-count: 1;-en-paragraph:true;">   </span> <span style="-en-paragraph:true;">struct in6_addr sin6_addr;</span><span style="mso-tab-count: 3;-en-paragraph:true;">        </span> <span style="-en-paragraph:true;">/* IPv6 address */</span></div><div style="margin-top: 1em; margin-bottom: 1em;"><span style="mso-tab-count: 1;-en-paragraph:true;">   </span> <span style="-en-paragraph:true;">__u32 sin6_scope_id;</span> <span style="mso-tab-count: 4;-en-paragraph:true;">             </span> <span style="-en-paragraph:true;">/* scope id (new in RFC2553) */</span></div><div style="margin-top: 1em; margin-bottom: 1em;"><span style="-en-paragraph:true;">};</span></div><div style="margin-top: 1em; margin-bottom: 1em;"><span style="-en-paragraph:true;"> </span></div><div style="margin-top: 1em; margin-bottom: 1em;"><span style="-en-paragraph:true;">struct in6_addr {</span></div><div style="margin-top: 1em; margin-bottom: 1em;"><span style="mso-tab-count: 1;-en-paragraph:true;">   </span> <span style="-en-paragraph:true;">union {</span></div><div style="margin-top: 1em; margin-bottom: 1em;"><span style="mso-tab-count: 2;-en-paragraph:true;">       </span> <span style="-en-paragraph:true;">__u8 u6_addr8[16];</span></div><div style="margin-top: 1em; margin-bottom: 1em;"><span style="mso-tab-count: 2;-en-paragraph:true;">       </span> <span style="-en-paragraph:true;">__be16 u6_addr16[8];</span></div><div style="margin-top: 1em; margin-bottom: 1em;"><span style="mso-tab-count: 2;-en-paragraph:true;">       </span> <span style="-en-paragraph:true;">__be32 u6_addr32[4];</span></div><div style="margin-top: 1em; margin-bottom: 1em;"><span style="mso-tab-count: 1;-en-paragraph:true;">   </span> <span style="-en-paragraph:true;">} in6_u;</span></div><div style="margin-top: 1em; margin-bottom: 1em;"><span style="mso-tab-count: 1;-en-paragraph:true;">   </span> <span style="-en-paragraph:true;">#define s6_addr</span> <span style="mso-tab-count: 2;-en-paragraph:true;">      </span> <span style="-en-paragraph:true;">in6_u.u6_addr8</span></div><div style="margin-top: 1em; margin-bottom: 1em;"><span style="mso-tab-count: 1;-en-paragraph:true;">   </span> <span style="-en-paragraph:true;">#define s6_addr16</span> <span style="mso-tab-count: 2;-en-paragraph:true;">    </span> <span style="-en-paragraph:true;">in6_u.u6_addr16</span></div><div style="margin-top: 1em; margin-bottom: 1em;"><span style="mso-tab-count: 1;-en-paragraph:true;">   </span> <span style="-en-paragraph:true;">#define s6_addr32</span><span style="mso-tab-count: 1;-en-paragraph:true;"> </span> <span style="mso-tab-count: 1;-en-paragraph:true;">  </span> <span style="-en-paragraph:true;">in6_u.u6_addr32</span></div><div style="margin-top: 1em; margin-bottom: 1em;"><span style="-en-paragraph:true;">};</span></div><div style="margin-top: 1em; margin-bottom: 1em;"><span style="-en-paragraph:true;"> </span></div><div style="margin-top: 1em; margin-bottom: 1em;"><span style="-en-paragraph:true;">#define UNIX_PATH_MAX 108</span></div><div style="margin-top: 1em; margin-bottom: 1em;"><span style="mso-tab-count: 1;-en-paragraph:true;">   </span> <span style="-en-paragraph:true;">struct sockaddr_un {</span></div><div style="margin-top: 1em; margin-bottom: 1em;"><span style="mso-tab-count: 1;-en-paragraph:true;">   </span> <span style="-en-paragraph:true;">__kernel_sa_family_t sun_family;</span> <span style="mso-tab-count: 1;-en-paragraph:true;"> </span> <span style="-en-paragraph:true;">/* AF_UNIX */</span></div><div style="margin-top: 1em; margin-bottom: 1em;"><span style="mso-tab-count: 1;-en-paragraph:true;">   </span> <span style="-en-paragraph:true;">char sun_path[UNIX_PATH_MAX]; /* pathname */</span></div><div><span style="-en-paragraph:true;">};</span></div><div><br/></div><div>通用套接字结构体</div><div style="margin-top: 1em; margin-bottom: 1em;"><span style="-en-paragraph:true;">struct sockaddr {</span></div><div style="margin-top: 1em; margin-bottom: 1em;"><span style="mso-tab-count: 1;-en-paragraph:true;">   </span> <span style="-en-paragraph:true;">sa_family_t sa_family;</span> <span style="mso-tab-count: 2;-en-paragraph:true;">   </span> <span style="-en-paragraph:true;">/* address family, AF_xxx */</span></div><div style="margin-top: 1em; margin-bottom: 1em;"><span style="mso-tab-count: 1;-en-paragraph:true;">   </span> <span style="-en-paragraph:true;">char sa_data[14];</span><span style="mso-tab-count: 3;-en-paragraph:true;">         </span> <span style="-en-paragraph:true;">/* 14 bytes of protocol address */</span></div><div><span style="-en-paragraph:true;">};</span></div><div><br/></div><div><br/></div><div>13 tcp</div><div>传输控制协议</div><div>特点:   出错重传 每次发送数据对方都会回ACK, 可靠 </div><div>tcp是抽象打电话的模型</div><div>建立练级   使用连接   关闭连接</div><div><br/></div><div>14 创建套接字API</div><div>#include &lt;sys/socket.h&gt;</div><div>int socket(int domain, int type, int protocol);<br/></div><div>创建套接字</div><div>参数:</div><div>domain:AF_INET</div><div>type: SOCK_STREAM 流式套接字 用于tcp通信</div><div>protocol: 0</div><div>成功返回文件描述符,失败返回-1</div><div><br/></div><div>15  连接服务器</div><div>#include &lt;sys/socket.h&gt;</div><div> int connect(intsockfd , const struct sockaddr *addr,<br/></div><div>                   socklen_t addrlen);</div><div>功能: 连接服务器</div><div><br/></div><div>sockfd:  socket套接字</div><div>addr:  ipv4套接字结构体的地址</div><div>addrlen: ipv4套接字结构体的长度<br/></div><div><br/></div><div><br/></div><div><br/></div><div>16 tcp服务器通信流程</div><div><img src="01 网络基础概念_files/Image [9].png" type="image/png" data-filename="Image.png" width="532"/></div><div><img src="01 网络基础概念_files/Image [10].png" type="image/png" data-filename="Image.png"/></div><div><br/></div><div><img src="01 网络基础概念_files/Image [11].png" type="image/png" data-filename="Image.png"/></div><div><br/></div><div>17 bind绑定</div><div>给套接字绑定固定的端口和ip</div><div>#include &lt;sys/socket.h&gt;</div><div>  int bind(int sockfd, const struct sockaddr *addr,<br/></div><div>                socklen_t addrlen);</div><div><br/></div><div>sockfd: 套接字</div><div>addr: ipv4套接字结构体地址</div><div>addrlen: ipv4套接字结构体的大小</div><div>返回值:</div><div>成功返回0 失败返回;-1</div><div><br/></div><div>18  listen</div><div>#include &lt;sys/socket.h&gt;</div><div>int listen(int sockfd, int backlog);<br/></div><div>参数:</div><div><span>    sockfd : 套接字</span><br/></div><div><span><span>    backlog :  已完成连接队列和未完成连接队里数之和的最大值  128</span><br/></span></div><div><br/></div><div>19  accept</div><div>#include &lt;sys/socket.h&gt;</div><div> int accept(int socket, struct sockaddr *restrict address,<br/></div><div>           socklen_t *restrict address_len);</div><div>如果连接队列没有新的连接,accept会阻塞</div><div><br/></div><div>功能: 从已完成连接队列提取新的连接</div><div>参数:</div><div><span>    socket : 套接字</span><br/></div><div><span><span>    address : 获取的客户端的的ip和端口信息  iPv4套接字结构体地址</span><br/></span></div><div><span>    </span>address_len: iPv4套接字结构体的大小的地址</div><div>socklen_t len = sizeof(struct sockaddr );</div><div><br/></div><div>返回值:  新的已连接套接字的文件描述符</div><div><br/></div><div>20  tcp服务器通信步骤</div><div>1 创建套接字  socket</div><div>2 绑定  bind</div><div>3  监听 listen</div><div>4  提取  accept</div><div>5 读写</div><div>6 关闭 </div><div><br/></div><div><br/></div><div><br/></div><div><br/></div><div><br/></div><div><br/></div><div><br/></div><div><br/></div><div><br/></div></span>
</div></body></html> 